<?php 
use yii\helpers\Html;
use yii\widgets\LinkPager;
use yii\widgets\ActiveForm;
?>
<!-- 权限管理模块 -->
	<div class="check-div">
		<button class="btn btn-yellow btn-xs" data-toggle="modal" data-target="#addNode">添加权限</button>
	</div>
	<div class="data-div">
		<div class="row tableHeader">
			<div class="col-xs-2 ">
				I D
			</div>
			<div class="col-xs-3">
				角色名
			</div>
			<div class="col-xs-4">
				角色所属权限
			</div>
			<div class="col-xs-3">
				操作
			</div>
		</div>
		<div class="tablebody">
		<?php foreach ($role_data as $k => $v) {?>
			<div class="row">
				<div class="col-xs-2 ">
					<?=$v['id'] ?>
				</div>
				<div class="col-xs-3">
					</span><span><?=$v['name'] ?></span>
				</div>
				<div class="col-xs-4">
					<?php if($v['node_id'] != ''){ 
						echo $v['node_id'];
					}else{
						echo "未分配权限";
						} ?>
				</div>
				<div class="col-xs-3">
					<button class="btn btn-success btn-xs update" data-toggle="modal" role_id='<?=$v["id"]?>' data-target="#changeNode">修改</button>
					<button class="btn btn-danger btn-xs delete" data-toggle="modal"  role_id='<?=$v["id"]?>'  data-target="#deleteNode">删除</button>
				</div>
			</div>
		<?php } ?>
		</div>

	</div>
		<footer class="footer">
		<ul class="pagination">
			<li>
				<select id="page">
					<?php for ($i=1; $i <=$page->pageCount ; $i++) { 
						if ($i == $nowPage) {
						echo "<option selected>".$nowPage."</option>";
					}else{?>

						<option><?=$i?></option>
					<?php }} ?>
				</select>
				页
			</li>
			<li class="gray">
				共<?=$page->pageCount?>页
			</li>
			<?=LinkPager::widget(['pagination' =>$page])?>
		</ul>
	</footer>
	<!--页码块-->
	<div class="modal fade" id="addNode" role="dialog" aria-labelledby="gridSystemModalLabel">
		<div class="modal-dialog" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
					<h4 class="modal-title" id="gridSystemModalLabel">添加角色</h4>
				</div>
				<?php $form = ActiveForm::begin(['action' => ['add_role']])?>
				<div class="modal-body">
					<div class="container-fluid">
						

					        <?= $form->field($RoleForm, 'name') ?>
					        <label class="control-label" for="adminrole-name">权限</label>
					        <div id='AdminRole[node_id]'>
					       		<?php foreach ($NodeData as $k => $v) { ?>
					        	<label>
					        		<input name="AdminRole[node_id][]" type="checkbox" value='<?= $v['id']?>'>
					        		<?=$v['name']?>
					        	</label>
					        	<?php } ?>
					        </div>

							
					   
					</div>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn" data-dismiss="modal">取 消</button>
					<?= Html::submitButton('添加', ['class' => 'btn btn-primary']) ?>
				</div>
				 <?php ActiveForm::end(); ?>
			</div>
			<!-- /.modal-content -->
		</div>
		<!-- /.modal-dialog -->
	</div>
	<script>
		$(function(){
			$("#page").change(function(){
				var page = $(this).val()
				location.href='http://www.lhl.com/nine/Part-time job/backend/web/index.php?w=role%2Frole_lists&page='+page;
			})
		})
	</script>
	<!--增加权限弹出窗口-->

	<!-- /.modal -->

	<!--修改权限弹出窗口-->
		<div class="modal fade" id="changeNode" role="dialog" aria-labelledby="gridSystemModalLabel">
		<div class="modal-dialog" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
					<h4 class="modal-title" id="gridSystemModalLabel">修改角色</h4>
				</div>
				<?php $form = ActiveForm::begin(['action' => ['update_role']])?>
				<div class="modal-body">
					<div class="container-fluid">
						
							<?= Html::tag('input','',['type'=>'hidden' ,'','name'=>"id",'ID'=>'admin_role-id'])?>
					        <?= $form->field($RoleForm, 'name')->textInput(['id'=>'name']) ?>
					        <label class="control-label" for="adminrole-name">权限</label>
					        <div id='AdminRole[node_id]'>
					       		<?php foreach ($NodeData as $k => $v) { ?>
					        	<label>
					        		<input class="node" name="AdminRole[node_id][]" type="checkbox" value='<?= $v['id']?>'>
					        		<?=$v['name']?>
					        	</label>
					        	<?php } ?>
					        </div>

							
					   
					</div>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn" data-dismiss="modal">取 消</button>
					<?= Html::submitButton('修改', ['class' => 'btn btn-primary']) ?>
				</div>
				 <?php ActiveForm::end(); ?>
			
	
			</div>
			<!-- /.modal-content -->
		</div>
		<!-- /.modal-dialog -->
	</div>
	<!-- /.modal -->
<script>
		$(function(){
			$(".update").click(function(){
				var role_id = $(this).attr('role_id')
				var _csrf  = $('input[name="_csrf"]').val()
				$('#admin_role-id').val(role_id);
				// $('input[type="checkbox"]').attr('checked',false)
				$.ajax({
					type:"post",
					url:"index.php?w=role/findone",
					data:{
						'_csrf':_csrf,
						'id':role_id
					},
					dataType:'json',
					success:function(data){
						if (data.error) {
							$('#name').val(data.msg.name);
							var num = $('.node').length
							for (var i = 0; i <  num; i++) {

								var checked = $('.node').eq(i).val()
								
								var arr = data.msg.node_id.split(",");
								if ($.inArray(checked,arr) != -1) {
									$('.node').eq(i).attr('checked',true)
								}else{
									$('.node').eq(i).attr('checked',false)
								}
							};
						}
						else{
							alert(data.msg)
						}
					}
				})
			})
		})
	</script>
	<!--弹出删除权限警告窗口-->
	<div class="modal fade" id="deleteNode" role="dialog" aria-labelledby="gridSystemModalLabel">
		<div class="modal-dialog" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
					<h4 class="modal-title" id="gridSystemModalLabel">提示</h4>
				</div>
				<div class="modal-body">
					<div class="container-fluid">
						<span id="del_content">确定要删除该权限？删除后不可恢复！</span>
					</div>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-xs btn-white" data-dismiss="modal">取 消</button>
					<button type="button" class="btn btn-xs btn-danger true_delete" role_id=''>确 定</button>
				</div>
			</div>
			<!-- /.modal-content -->
		</div>
		<!-- /.modal-dialog -->
	</div>
	<!-- /.modal -->
<script>
	$(function(){

		$(".delete").click(function(){
			var role_id = $(this).attr('role_id')
			$(".btn-danger").attr('role_id',role_id)
		})
		//隐藏提示框
		$('.btn-white').click(function(){
			$('#deleteNode').css("display","none;")
		})
		$(".true_delete").click(function(){
			$('#del_content').html('确定要删除该权限？删除后不可恢复！');
			var role_id = $(this).attr('role_id')
			var url = window.location.href;
			var _csrf  = $('input[name="_csrf"]').val()
			$.ajax({
				type:"post",
				url:"index.php?w=role/role_delete",
				data:{
					'_csrf':_csrf,
					'url':url,
					'role_id':role_id
				},
				dataType:"json",
				success:function(data){
					if (!data.error) {
						alert(data.msg)
					}else{
						location.href=data.msg;
					}
				}
			})
		})
	})
</script>